package com.timber.util;

import java.util.*;


public class ITPPBAllocationMgr
{
  private static boolean DEBUG = false;

  /* Stored proc call to retrieve PB Streetside and
  *  Contract Allocation function states for Trade Entry pt -
  *  State depends on which Trade Entry UI being used to enter the data
  *  and values of various data fields of the trade being entered
  */

  public static SLDataMgr mgr = new SLDataMgr( "com.db.gef.util.ITPPBAllocation", "common..p_GetPBAllocationMatrix" );

  public static void init() throws Exception
  {
    mgr.clear();
    mgr.runProc();
  }

  /* Return a specific PB/Allocation state (or null) and function state values (ITPPBAllocation object);
  *  Pass in key:
  *   (<Trade Entry UI>-<IntercoInd>-<Cpty|Entity>-<Manual|Auto|None>-<Special>)
  */
  public static ITPPBAllocation getITPPBAllocation( Object key )
  {
    ITPPBAllocation ret = null;

    try
    {
      ret = ( ITPPBAllocation )mgr.getByKey( key );
    }
    catch( Exception ex )
    {
      ret = null;
    }
    return ret;
  }


  /* Return a Hashmap of trade entry attributes and related function states (or null);
  *  Pass in parameters that combine to makeup the
  *  key to the specific PBAllocation object held in the SLDataMgr hashmap;
  *  PBAllocation object data will be used to create and
  *  return the hashmap containing key and state values
  *  for PBStreetside function and the Contract Allocation funciton
  */

  public static HashMap getPBAllocationState(String ui, String interco, String viewpt, String allocationType, String special)
  {

    String hashKey;

    hashKey = (String)buildHashKeyForLookup(ui, interco, viewpt, allocationType, special);
    if (hashKey == null)    { return null; }

    return (getPBAllocationState(hashKey));
  }

  public static HashMap getPBAllocationState(Object key)
  {
    ITPPBAllocation ret = null;
    HashMap hm = new HashMap(100);

    ret = getITPPBAllocation((String)key);
    if (ret == null)        { return null; }

    hm.put( ITPPBAllocation.PB_STREETSIDE_STATE, ret.getPBStreetsideState() );
    hm.put( ITPPBAllocation.ALLOCATION_STATE, ret.getAllocationState() );

    return hm;
  }


  /* Return true/false to whether function is enabled for
  *  a given trade entry scenario;
  *  Pass in SLContract object and the trade action being evaluated
  */

  public static boolean isFunctionEnabled(String function, String ui, String interco, String viewpt, String allocationType, String special)
  {
      String hashKey  = null;

      hashKey = (String)buildHashKeyForLookup(ui, interco, viewpt, allocationType, special);
          if (hashKey == null)    { return false; }

      return(isFunctionEnabled(function, hashKey));
  }

  public static boolean isFunctionEnabled(String function, Object key)
  {

      ITPPBAllocation ret = null;

      if (function == null)         { return false; }

      if (ITPPBAllocation.isFunctionValid(function))
      {
          ret = getITPPBAllocation((String)key);
          if (ret == null)        { return false; }

          if (function.equals(ITPPBAllocation.PB_STREETSIDE_STATE) )
          {
              if ( (ret.getPBStreetsideState()).equals("E") )      { return true; }
          }
          if (function.equals(ITPPBAllocation.ALLOCATION_STATE) )
          {
              if ( (ret.getAllocationState()).equals("E") )        { return true; }
          }
      }
      return false;
  }


  /* Return String used as the key for lookup of ITPPBAllocation object (or null);
   *  Pass in the parameters and use to format the lookup key
   */

  public static Object buildHashKeyForLookup(String ui, String interco, String viewpt, String allocationType, String special)
  {
      String tradeEntryUI           = ui;
      String intercoInd             = interco;
      String viewpoint              = viewpt;
      String costCenterAllocation   = allocationType;
      String specialInd             = special;

      StringBuffer lookupKey = new StringBuffer();

      if ( tradeEntryUI != null )
      {
          lookupKey.append( tradeEntryUI);
      }

      if ( intercoInd != null )
      {
          lookupKey.append( ITPPBAllocation.HASH_DELIM);
          lookupKey.append( intercoInd);
      }

      if ( viewpoint != null )
      {
          lookupKey.append( ITPPBAllocation.HASH_DELIM);
          lookupKey.append( viewpoint);
      }

      if ( costCenterAllocation != null )
      {
          lookupKey.append( ITPPBAllocation.HASH_DELIM);
          lookupKey.append( costCenterAllocation);
      }

      if ( specialInd != null )
      {
          lookupKey.append( ITPPBAllocation.HASH_DELIM);
          lookupKey.append( specialInd);
      }

      if (lookupKey == null)            { return null; }


      if (DEBUG)
      {
          Util.debug("Function State lookup key: " + lookupKey.toString());
      }

      return   lookupKey.toString();

  }



  public static Vector getItems() throws Exception
  {
    return mgr.getKeyList();
  }

  public static Vector getDataVector() throws Exception
  {
    return mgr.getDataVector();
  }




}